/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.com.transportes.servlet;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Blob;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import pe.com.transportes.modelo.Bus;
import pe.com.transportes.service.ConsultaService;

/**
 *
 * @author z3phiroth
 */
public class AgregarBusServlet extends javax.servlet.http.HttpServlet
		implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;


   	public AgregarBusServlet() {
		super();
	}









    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet AgregarBusServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet AgregarBusServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
   
            ConsultaService cs = new ConsultaService();
            String cod = request.getParameter("cod");
            String placa = request.getParameter("placa");
            String marca = request.getParameter("marca");
            String estado = request.getParameter("estado");
            String nroasientos = request.getParameter("nroasientos");
            String descripcion = request.getParameter("descripcion");
           
       String imagen = request.getParameter("campoimagen");

                        System.out.println("codigo servlet es" + cod);
                        Bus bean = new Bus();
                        bean.setCod_bus(new Integer(cod));
                        bean.setPlaca(placa);
                        bean.setMarca(marca);
                        bean.setEstado(estado);
                        bean.setNroasientos(new Integer(nroasientos));
                        bean.setDescripcion(descripcion);
                      bean.setImagen_bus(imagen);
           
                    System.out.println("Archivo guardado");
           cs.RegistrarBus(bean);
        
            String pagina = "/registrarbuses.jsp";
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(pagina);
            dispatcher.forward(request, response);
        
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
